<template>
  <div class="app-container">
    <div>
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label="用户名：" prop="title">
          <el-input v-model="searchForm.title" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="状态：" prop="state">
          <el-select v-model="searchForm.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="fetchData(true)">查询</el-button>
          <el-button type="danger" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="id" width="95" prop="id" />
      <el-table-column label="房屋名称" width="150" prop="houseInfo.title" />
      <el-table-column label="用户" width="150" prop="userInfo.username" />
      <el-table-column label="联系方式" width="150" prop="userInfo.phone" />
      <el-table-column label="租赁期限（年）" width="150" prop="term" />
      <el-table-column label="租赁金额" width="150" prop="totalPrice" />
      <el-table-column label="状态" prop="state">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state === 0 ? 'danger' : ''">{{
            options.filter((v) => v.value === scope.row.state)[0].label
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="操作" prop="op">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row)"
            >查看</el-button
          >
          <el-button type="danger" @click="handleDel(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :page-sizes="paginationConfig.pageSizes"
      :layout="paginationConfig.layout"
      :total="paginationConfig.total"
      :page="paginationConfig.page"
      :limit="paginationConfig.pageSize"
      @pagination="handleChangePage"
    />
  </div>
</template>

<script>
import { deleteOrder, getOrderList } from "@/api/order";

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        state: "",
      },
      options: [
        {
          label: "全部",
          value: "",
        },
        {
          label: "待支付",
          value: 0,
        },
        {
          label: "已支付，待审核",
          value: 1,
        },
        {
          label: "入住中",
          value: 2,
        },
        {
          label: "已退租",
          value: 3,
        },
      ],
      paginationConfig: {
        direction: "center", // 分页位置
        layout: "total, sizes, prev, pager, next, jumper", // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10, // 每页条数-默认10
      },
      curryNew: {},
      dialogNewsDetailVisible: false,
    };
  },
  created() {
    this.fetchData(true);
  },
  methods: {
    fetchData(isClear = false) {
      if (isClear) {
        this.paginationConfig.page = 1;
      }
      this.listLoading = true;
      const { page, pageSize } = this.paginationConfig;
      const obj = {
        page,
        pageSize,
      };
      getOrderList({
        ...obj,
        ...this.searchForm,
      }).then((response) => {
        if (response.code === 200) {
          response.data.list.map((item) => {
            item.totalPrice = (item.totalPrice / 100).toFixed(2);
          });
          this.list = response.data.list;
          this.paginationConfig.total = response.data.total;
          this.listLoading = false;
        }
      });
    },
    handleChangePage({ page, pageSize }) {
      this.paginationConfig.page = page;
      this.paginationConfig.pageSize = pageSize;
      this.fetchData();
    },

    onReset() {
      this.$refs.searchFormRef.resetFields();
      this.fetchData();
    },
    handleEdit(e) {
      this.$router.push({
        name: "orderDetail",
        query: {
          id: e.id,
          type: "edit",
        },
      });
    },
    onAdd() {
      this.$router.push({
        name: "orderDetail",
        query: {
          type: "add",
        },
      });
    },
    async handleDel(e) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await deleteOrder({
          id: e.id,
        });
        if (res.code === 200) {
          this.$message.success(res.data);
          this.fetchData();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.dialog-content {
  width: 375px;
  height: 600px;
  overflow: hidden;
  overflow-y: scroll;
}
</style>
